//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package roadmap_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/roadmap_state_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type UserNoteStateDbItem struct {
	Title string `bson:"title"`
}

type StateDataDbItem struct {
	TopicState    *NodeTopicStateDbItem    `bson:"topicState"`
	TodoListState *NodeTodoListStateDbItem `bson:"todoListState"`
	UserNoteState *UserNoteStateDbItem     `bson:"userNoteState"`
}

type StateOpItemDbItem struct {
	OpId         string          `bson:"_id"`
	RoadmapId    string          `bson:"roadmapId"`
	RoadmapTitle string          `bson:"roadmapTitle"`
	NodeId       string          `bson:"nodeId"`
	OpType       uint32          `bson:"opType"`
	TimeStamp    int64           `bson:"timeStamp"`
	UserId       string          `bson:"userId"`
	StateData    StateDataDbItem `bson:"stateData"`
}

func (item *StateOpItemDbItem) ToStateOpItem() *roadmap_state_api.StateOpItem {
	retItem := &roadmap_state_api.StateOpItem{
		OpId:         item.OpId,
		RoadmapId:    item.RoadmapId,
		NodeId:       item.NodeId,
		OpType:       roadmap_state_api.STATE_OP_TYPE(item.OpType),
		TimeStamp:    item.TimeStamp,
		RoadmapTitle: item.RoadmapTitle,
		UserId:       item.UserId,
	}
	if item.StateData.TopicState != nil {
		retItem.StateData = &roadmap_state_api.StateOpItem_TopicState{
			TopicState: &roadmap_state_api.NodeTopicState{
				State: roadmap_state_api.TOPIC_STATE_TYPE(item.StateData.TopicState.State),
				Title: item.StateData.TopicState.Title,
			},
		}
	} else if item.StateData.TodoListState != nil {
		doneList := []*roadmap_state_api.NodeTodoStateItem{}
		for _, todoItem := range item.StateData.TodoListState.DoneList {
			doneList = append(doneList, &roadmap_state_api.NodeTodoStateItem{
				TodoId: todoItem.TodoId,
				Title:  todoItem.Title,
			})
		}
		retItem.StateData = &roadmap_state_api.StateOpItem_TodoListState{
			TodoListState: &roadmap_state_api.NodeTodoListState{
				DoneList: doneList,
			},
		}
	} else if item.StateData.UserNoteState != nil {
		retItem.StateData = &roadmap_state_api.StateOpItem_UserNoteState{
			UserNoteState: &roadmap_state_api.UserNoteState{
				Title: item.StateData.UserNoteState.Title,
			},
		}
	}
	return retItem
}

type _StateOpDao struct {
	mc *mongo.Collection
}

func newStateOpDao(db *mongo.Database) (*_StateOpDao, error) {
	mc := db.Collection(db_ns.DB_NS_ROADMAP + "state_op")
	return &_StateOpDao{mc: mc}, nil
}

func (db *_StateOpDao) Insert(ctx context.Context, dbItem *StateOpItemDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_StateOpDao) ListById(ctx context.Context, opIdList []string) ([]*StateOpItemDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": opIdList}}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*StateOpItemDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, nil
}
